#include "mainwidget.h"
#include "ui_mainwidget.h"

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    loadClient(Login);
    connect(pLoginWidget,SIGNAL(sendToMainWidget(Client)),this,SLOT(loginSuccessed(Client)));
    connect(this,SIGNAL(clicked(Position,int)),this,SLOT(response(Position,int)));

}

MainWidget::~MainWidget()
{
    pSinaWidget->~SinaWidget();
    delete ui;
}

void MainWidget::loadClient(Client client){

    if (client == Login && currentClient != Login){

        pLoginWidget = new LoginWidget(this);
        removeCurrentClient();
        ui->layout->addWidget(pLoginWidget);
        currentClient = Login;
    }

    else if (client == Sina && currentClient != Sina){
        removeCurrentClient();
        QPixmap pixmap(":/images/sina/LOGO_64x64.png");
        ui->label_up_1->setPixmap(pixmap);
        Account *pAccount = pLoginWidget->getSinaAccount();

        SinaWeibo *pSinaWeibo = new SinaWeibo(pAccount);
        pSinaWeibo->initHomePageStatus();
        pSinaWeibo->initEmotions();
        delete pSinaWeibo;

        pSinaWidget = new SinaWidget(pAccount,this);

        ui->layout->addWidget(pSinaWidget);

        currentClient = Sina;
        connect(this,SIGNAL(sendSignalToSina(int)),pSinaWidget,SLOT(receiveSignalFromMainWidget(int)));
    }
}

void MainWidget::mousePressEvent(QMouseEvent *pEvent){

    if (currentClient == Login){
        pEvent->ignore();
        return;
    }

    int x = pEvent->x();
    int y = pEvent->y();

    int reminderOfX = x % widget_width;
    int reminderOfY = y % widget_height;

    if ( (reminderOfX > distance && reminderOfX < widget_width - distance)
            &&(reminderOfY > distance && reminderOfY < widget_height - distance)
            &&( x / widget_width < 1 || y / widget_height < 1)
            &&  y / widget_height < 9
            &&  x / widget_width  < 16
            )
    {
        if ( x / widget_width < 1 && currentClient != None){
            qDebug("你点击了左边的第"+toQByteArray(y / widget_height)+"个Label");
            emit clicked(Left,y / widget_height);
        }
        else if ( y / widget_height < 1){
            qDebug("你点击了上边的第"+toQByteArray(x / widget_width)+"个Label");
            emit clicked(Up,x / widget_width);
        }
    }


}

void MainWidget::response(Position pos, int index){


    if ( pos == Left ){
        if ( index >0 && index < 9){
            emit sendSignalToSina(index);
        }
        else {
            qDebug("pos = left ,index value Error!!!");
            return ;
        }
    }
    else if ( pos == Up){
        switch ( index ){
        case 1:
            if (ui->up_1->isVisible()){
                Client client = Sina;
                loadClient(client);
            }
            break;
        case 2:
            if (ui->up_2->isVisible()){

            }
            break;
        case 3:
            if (ui->up_3->isVisible()){

            }
            break;
        case 4:
            if (ui->up_4->isVisible()){

            }
            break;
        case 5:
            if (ui->up_5->isVisible()){

            }
            break;
        case 6:
            if (ui->up_6->isVisible()){

            }
            break;
        case 7:
            if (ui->up_7->isVisible()){

            }
            break;
        case 8:
            if (ui->up_8->isVisible()){

            }
            break;
        case 9:
            if (ui->up_9->isVisible()){

            }
            break;
        case 10:
            if (ui->up_10->isVisible()){

            }
            break;
        case 11:
            if (ui->up_11->isVisible()){

            }
            break;
        case 12:
            if (ui->up_12->isVisible()){

            }
            break;
        case 13:
            if (ui->up_13->isVisible()){

            }
            break;
        case 14:
            if (ui->up_14->isVisible()){

            }
            break;
        case 15:
            if (ui->up_15->isVisible()){

            }
            break;
        default:
            break;
        }
    }
    else{
        qDebug("In response(Position pos, int index):Position Error!!!");
        return ;
    }
}

QByteArray MainWidget::toQByteArray(int num){
    return QString::number(num).toLocal8Bit();
}

void MainWidget::removeCurrentClient(){

    switch (currentClient){

    case None:
        break;
    case Login:
        if (!ui->layout->isEmpty()){
            ui->layout->removeWidget(pLoginWidget);
        }
        pLoginWidget->deleteLater();
        break;
    case Sina:
        if (!ui->layout->isEmpty()){
            ui->layout->removeWidget(pSinaWidget);
        }
    default:
        break;
    }
}

void MainWidget::loginSuccessed(Client client){

    if (client == Sina){
        loadClient(Sina);
        return ;
    }
}
